from datetime import date

from django import forms
from django.forms import ModelForm, fields, modelformset_factory, BaseModelFormSet
from django.forms import widgets as Fwidget
from django.utils import timezone

from site01 import models
from site01.models import DeviceType, Device, SubProcess, Employees, Process, EmployeeHours


# 在这里创建你的forms表单类

class DeviceTypeForm(ModelForm):
    class Meta:
        model = DeviceType
        fields = '__all__'
        labels = {
            'typename': '设备类型',
        }
        widgets = {
            'typename': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入设备类型名称'
            })
        }


class DeviceForm(ModelForm):
    class Meta:
        model = Device
        fields = ['name', 'sn', 'devicetype', 'process']
        labels = {
            'name': '名称',
            'sn': '编号',
            'devicetype': '类型',
            'process': '所属工序'
        }
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入设备名称'
            }),
            'sn': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入设备编号'
            }),
            'devicetype': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择类型'
            }),
            'process': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择所属的工序'
            })
        }


class ProcessForm(ModelForm):
    class Meta:
        model = Process
        fields = '__all__'
        labels = {
            'name': '工序名称',
        }
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入工序名称'
            }),
        }


class SubProcessForm(ModelForm):
    class Meta:
        model = SubProcess
        fields = '__all__'
        labels = {
            'name': '子工序名称',
            'process': '所属工序',
        }
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入新的子工序名称'
            }),
            'process': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择所属的工序'
            })
        }


class EmployeesForm(ModelForm):
    class Meta:
        model = Employees
        fields = '__all__'
        labels = {
            'jobnumber': '工号',
            'jobname': '姓名',
            'process': '默认工序',
            'properties': '性质',
            'status': '状态',
        }
        widgets = {
            'jobnumber': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入工号'
            }),
            'jobname': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入姓名'
            }),
            'process': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择所属的工序'
            }),
            'properties': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择员工性质'
            }),
            'status': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择员工状态'
            }),
        }


## 没有使用ModelForm方式来做表单
class EmployeeHoursModelForm(ModelForm):
    # process_list = Process.objects.values_list('name')
    # temp1 = []
    # for x in process_list:
    #     temp1.append(x[0])
    # PROCESS_CHOICES = tuple(temp1)
    workday = forms.DateField(widget=Fwidget.SelectDateWidget(), initial=date.today())

    class Meta:
        model = EmployeeHours
        fields = '__all__'
        # exclude = ['workday']
        localized_fields = '__all__'
        labels = {
            'employee': '姓名',
            'process': '工序',
            'hours': '工时',
            'workday': '日期'
        }
        widgets = {
            'employee': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择员工'
            }),
            'process': forms.Select(attrs={
                'class': 'form-control',
                'placeholder': '请选择员工工序'
            }),
            'hours': forms.NumberInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入工时'
            }),
            'workday': forms.SelectDateWidget(attrs={
                'class': 'form-control',
                'placeholder': '请选择日期'
            })
        }


class EmployeeHoursForm1(forms.Form):
    employee = fields.ChoiceField(
        widget=Fwidget.Select, label='姓名'
    )
    process = fields.ChoiceField(
        widget=Fwidget.Select
    )
    hours = fields.FloatField(
    )
    workday = fields.DateField(
        widget=Fwidget.SelectDateWidget,
        initial=timezone.now()  # 必须这样设置默认值才能自动选择当前日期
    )

    def __init__(self, *args, **kwargs):
        super(EmployeeHoursForm1, self).__init__(*args, **kwargs)
        # values_list这里只能查询两个字段，因为，choices选择框只有两个参数,否则就提示出错
        self.fields['employee'].choices = models.Employees.objects.filter(status=1).values_list('id', 'jobname')
        # self.fields['process'].choices = models.Process.objects.all().values_list('id', 'name')
        # 通过重写form类的init方法来实现批量添加样式
        for field in iter(self.fields):
            self.fields[field].widget.attrs.update({
                'class': 'form-control'
            })





